package com.springboot.service.impl;

import com.springboot.mapper.NavigationRecordMapper;
import com.springboot.model.NavigationRecord;
import com.springboot.model.NavigationRecordKey;
import com.springboot.service.TableBarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TableBarServiceImpl implements TableBarService {
    @Autowired
    NavigationRecordMapper navigationRecordMapper;

    @Override
    public NavigationRecord showTableBarNum(Integer userId) {
        NavigationRecord navigationRecord = new NavigationRecord();
        try {
            navigationRecord = navigationRecordMapper.selectMaxNumByUserId(userId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("TableBarServiceImpl 出错");
        }
        System.out.println("tableBar:"+navigationRecord.getTablebarId());
        return navigationRecord;
    }

    @Override
    public int addTableBar(NavigationRecordKey navigationRecordKey) {
        int flag = 1;
        int success=1;
        NavigationRecord navigationRecord = new NavigationRecord();
        try {
            navigationRecord = navigationRecordMapper.selectByPrimaryKey(navigationRecordKey);
            Integer num = navigationRecord.getNum();
            navigationRecord.setNum(num+1);
        }
        catch (Exception e)
        {
            flag=0;
            navigationRecord = new NavigationRecord();
            navigationRecord.setNum(1);
            navigationRecord.setUserId(navigationRecordKey.getUserId());
            navigationRecord.setTablebarId(navigationRecordKey.getTablebarId());
            try {
                int a = navigationRecordMapper.insert(navigationRecord);
            }
            catch (Exception e1)
            {
                success=0;
                System.out.println("TableBarServiceImpl 插入失败");
            }
        }
        finally {
            try {
                int a = navigationRecordMapper.updateByPrimaryKey(navigationRecord);
            }
            catch (Exception e1)
            {
                success=0;
                System.out.println("TableBarServiceImpl 更新失败");
            }
            return success;
        }
    }
}
